﻿namespace RestaurantMenu.Library
{
    using System;
    using System.Collections.Generic;

    public class ProductList
    {
        private readonly List<ProductQuantity> allProducts;

        public List<ProductQuantity> AllProducts
        {
            get { return allProducts; }
        }

        public void AddProduct(Product product, int quantity)
        {
            if (quantity < 0)
            {
                throw new ArgumentNullException("The quantity cannot be negative!");
            }

            if (allProducts.Count == 0)
            {
                allProducts.Add(new ProductQuantity(product, quantity));
                return;
            }

            for (int index = 0; index < allProducts.Count; index++)
            {
                if (allProducts[index].Product == product)
                {
                    allProducts[index] = new ProductQuantity(product, allProducts[index].Quantity + quantity);//FIX
                    break;
                }
            }

            allProducts.Add(new ProductQuantity(product, quantity));

        }

        public void RemoveProduct(Product product)
        {
            for (int index = 0; index < allProducts.Count; index++)
            {
                if (allProducts[index].Product == product)
                {
                    if (allProducts[index].Quantity < 2)
                    {
                        allProducts.RemoveAt(index);
                    }
                    else
                    {
                        allProducts[index] = new ProductQuantity(product, allProducts[index].Quantity - 1);//FIX                    }
                    }
                }
            }
        }

        public ProductList()
        {
            allProducts = new List<ProductQuantity>();
        }
    }
}
